package com.beiding.workordersystem.orders.looporder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.beiding.workordersystem.common.dao.MPage;
import com.beiding.workordersystem.common.standard.E;
import com.beiding.workordersystem.common.standard.R;
import com.beiding.workordersystem.common.utils.MyBeanUtils;
import com.beiding.workordersystem.orders.looporder.entity.LoopTask;
import com.beiding.workordersystem.orders.looporder.service.LoopTaskService;
import com.beiding.workordersystem.orders.looporder.vo.LoopTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/loopTask")
public class LoopTaskController {

    @Autowired
    private LoopTaskService loopTaskService;

    @GetMapping("/page")
    R page(MPage page, String searchKey) {
        IPage iPage = loopTaskService.page(page.toIPage(),searchKey);
        return R.ok(iPage);
    }

    @PostMapping("/save")
    R save(@RequestBody @Validated LoopTaskVo vo) {
        LoopTask copy = MyBeanUtils.copy(vo, LoopTask.class);
        loopTaskService.save(copy);
        return R.ok(copy.getId());
    }

    @PostMapping("/delete")
    R delete(@RequestBody String[] ids) {
        loopTaskService.delete(ids);
        return R.ok();
    }

    @PostMapping("/start")
    R start(@RequestBody String[] ids) {
        loopTaskService.start(ids);
        return R.ok();
    }

    //续费处理
    @PostMapping("/postpone")
    R postpone(@RequestBody Map<String, Object> map) {
        String id = (String) map.get("id");
        Integer moon = (Integer) map.get("moon");
        E.notNull(id,"参数缺失");
        E.notNull(moon,"参数缺失");

        loopTaskService.postpone(id, moon);

        return R.ok();
    }



}
